# Description:
#
package(
    default_visibility = ["//visibility:public"],
)

licenses(["notice"])  # Apache 2.0

load(
    "//mace:mace.bzl",
    "if_android",
    "if_android_armv7",
    "if_bfloat16_enabled",
    "if_fp16_enabled",
    "if_hexagon_enabled",
    "if_neon_enabled",
    "if_opencl_enabled",
    "if_quantize_enabled",
)

cc_library(
    name = "common",
    srcs = glob(
        [
            "common/*.cc",
            "delegator/*.cc",
        ],
    ),
    hdrs = glob(
        [
            "common/*.h",
            "delegator/*.h",
        ],
    ),
    copts = [
        "-Werror",
        "-Wextra",
        "-Wno-missing-field-initializers",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon-fp16",
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_bfloat16_enabled([
        "-DMACE_ENABLE_BFLOAT16",
    ]) + if_fp16_enabled([
        "-DMACE_ENABLE_FP16",
        "-march=armv8.2-a+fp16",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]),
    deps = [
        "//mace/core",
    ],
)

cc_library(
    name = "ref_kernels",
    srcs = glob(
        [
            "ref/*.cc",
        ],
    ) + if_quantize_enabled(glob([
        "ref/q8/*.cc",
    ])),
    hdrs = glob(
        [
            "ref/*.h",
        ],
    ) + if_quantize_enabled(glob([
        "ref/q8/*.h",
    ])),
    copts = [
        "-Werror",
        "-Wextra",
        "-Wno-missing-field-initializers",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon-fp16",
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_bfloat16_enabled([
        "-DMACE_ENABLE_BFLOAT16",
    ]) + if_fp16_enabled([
        "-DMACE_ENABLE_FP16",
        "-march=armv8.2-a+fp16",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]),
    deps = [
        ":common",
        "//mace/core",
    ],
)

# After refactor, all arm neon kernels go here.
# Could be shipped to other product use.
cc_library(
    name = "arm_neon_kernels",
    srcs = glob(
        [
            "arm/base/*.cc",
            "arm/fp32/*.cc",
            "arm/fp16/gemv.h",
        ],
    ) + if_quantize_enabled(glob(
        [
            "arm/q8/*.cc",
        ],
    )) + if_bfloat16_enabled(glob(
        [
            "arm/bf16/*.cc",
        ],
    )) + if_fp16_enabled(glob(
        [
            "arm/fp16/*.cc",
        ],
    )),
    hdrs = glob(
        [
            "arm/base/*.h",
            "arm/fp32/*.h",
        ],
    ) + if_quantize_enabled(glob(
        [
            "arm/q8/*.h",
        ],
    )) + if_bfloat16_enabled(glob(
        [
            "arm/bf16/*.h",
        ],
    )) + if_fp16_enabled(glob(
        [
            "arm/fp16/*.h",
        ],
    )),
    copts = [
        "-Werror",
        "-Wextra",
        "-Wno-missing-field-initializers",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon-fp16",
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_bfloat16_enabled([
        "-DMACE_ENABLE_BFLOAT16",
    ]) + if_fp16_enabled([
        "-DMACE_ENABLE_FP16",
        "-march=armv8.2-a+fp16",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]),
    deps = [
        ":common",
        "//mace/core",
    ],
)

# After refactor, all GPU OpenCL kernels go here.
# Could be shipped to other product use.
cc_library(
    name = "opencl_kernels",
    srcs = glob(
        [
            "opencl/*.cc",
            "opencl/**/*.cc",
        ],
    ),
    hdrs = glob(
        [
            "opencl/*.h",
            "opencl/**/*.h",
        ],
    ),
    copts = [
        "-Werror",
        "-Wextra",
        "-Wno-missing-field-initializers",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon-fp16",
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]),
    deps = [
        ":common",
        "//mace/core",
    ],
)

cc_library(
    name = "internal_ops",
    srcs = glob(
        [
            "*.cc",
        ],
    ),
    hdrs = glob(
        [
            "*.h",
        ],
        exclude = [
            "fixpoint.h",
            "common/gemmlowp_util.h",
            "quantization_util.h",
        ],
    ) + if_quantize_enabled(glob([
        "fixpoint.h",
        "common/gemmlowp_util.h",
        "quantization_util.h",
    ])),
    copts = [
        "-Werror",
        "-Wextra",
        "-Wno-missing-field-initializers",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon-fp16",
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_bfloat16_enabled([
        "-DMACE_ENABLE_BFLOAT16",
    ]) + if_fp16_enabled([
        "-DMACE_ENABLE_FP16",
        "-march=armv8.2-a+fp16",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]),
    linkopts = if_android(["-lm"]),
    deps = [
        ":ref_kernels",
        "//mace/core",
    ] + if_quantize_enabled([
        "@tflite",
        "@gemmlowp",
    ]) + if_neon_enabled([
        ":arm_neon_kernels",
    ]) + if_opencl_enabled([
        ":opencl_kernels",
    ]),
)

cc_library(
    name = "ops",
    srcs = glob([
        "registry/*.cc",
    ]),
    hdrs = glob([
        "registry/*.h",
    ]),
    copts = [
        "-Werror",
        "-Wextra",
        "-Wno-missing-field-initializers",
    ] + if_neon_enabled([
        "-DMACE_ENABLE_NEON",
    ]) + if_android_armv7([
        "-mfpu=neon-fp16",
        "-mfloat-abi=softfp",
    ]) + if_opencl_enabled([
        "-DMACE_ENABLE_OPENCL",
    ]) + if_quantize_enabled([
        "-DMACE_ENABLE_QUANTIZE",
    ]) + if_bfloat16_enabled([
        "-DMACE_ENABLE_BFLOAT16",
    ]) + if_fp16_enabled([
        "-DMACE_ENABLE_FP16",
        "-march=armv8.2-a+fp16",
    ]) + if_hexagon_enabled([
        "-DMACE_ENABLE_HEXAGON",
    ]),
    linkopts = if_android(["-lm"]),
    deps = [
        "internal_ops",
    ],
)
